//
//  SurpriseDetailBottomView.swift
//  XiaoJingMallSwift
//
//  Created by beisheng03 on 2017/9/4.
//  Copyright © 2017年 beisheng. All rights reserved.
//

import UIKit

class SurpriseDetailBottomView: UIView {

    @IBOutlet weak var middleLabel: UILabel!
    @IBOutlet weak var addBtn: UIButton!
    @IBOutlet weak var subBtn: UIButton!
    @IBOutlet weak var bgView: UIView!
    @IBOutlet weak var middleBtn: UIButton!
    @IBOutlet weak var rightBtn: UIButton!
    weak var delegate : SurprisDetailBottomDelegate?{
        
        didSet{
            
            if delegate == nil {
                return
            }
            
            self.VC = delegate as? SurpriseShopDetailVC
            
        }
        
    }
    
    weak var VC : SurpriseShopDetailVC?
    
    
    
    
    
    @IBAction func rightBtnDidTap(_ sender: UIButton) {
        
        self.delegate?.rightBuyBtnDidTap(str: middleLabel.text!)
        
        
        
    }
    
    @IBAction func subBtnDidTap(_ sender: UIButton) {
        
        
        var number = Int(middleLabel.text!) ?? 1
        
        number -= 1
        
        middleLabel.text = "\(number)"
        
        if number == 1 {
            subBtn.isEnabled = false
        }
        
        if addBtn.isEnabled == false {
            addBtn.isEnabled = true
        }

        
    }
    
    @IBAction func middleBtnDidTap(_ sender: UIButton) {
        
        middleLabel.text = VC?.dataModel?.od_count
        
        addBtn.isEnabled = false
        
        subBtn.isEnabled = true
        
    }
    
    
    @IBAction func addBtnDidTap(_ sender: UIButton) {
        
        var number = Int(middleLabel.text!) ?? 1
        
        number += 1
        
        subBtn.isEnabled = true
        
        middleLabel.text = "\(number)"
        
        
        let number2 = Int(VC!.dataModel!.od_count)

        
        if number == number2 {
            
            addBtn.isEnabled = false
            
        }

        
        
    }
    
    
    override func awakeFromNib() {
        
        super.awakeFromNib()
        rightBtn.addCornerAndBorder(borderColor: nil, borderWidth: 0, masksToBounds: true, cornerRadius: 5)
        middleBtn.addCornerAndBorder(borderColor: nil, borderWidth: 0, masksToBounds: true, cornerRadius: 5)
        
        bgView.addCornerAndBorder(borderColor: UIColor.lineGray(), borderWidth: 1, masksToBounds: true, cornerRadius: 3)
        
        bgView.backgroundColor = UIColor.lineGray()
        
        addBtn.setTitleColor(UIColor.themeColor(), for: .normal)
        addBtn.setTitleColor(UIColor.lightGray, for: .disabled)
        addBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 5, 0)
        addBtn.setBackgroundImage(UIImage.imageWithColor(color: UIColor.colorWith(r: 234, g: 234, b: 234)), for: .disabled)
        addBtn.setBackgroundImage(UIImage.imageWithColor(color: UIColor.white), for: .normal)
        
        subBtn.setTitleColor(UIColor.themeColor(), for: .normal)
        subBtn.setTitleColor(UIColor.lightGray, for: .disabled)
        subBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 2, 0)
        subBtn.setBackgroundImage(UIImage.imageWithColor(color: UIColor.colorWith(r: 234, g: 234, b: 234)), for: .disabled)
        subBtn.setBackgroundImage(UIImage.imageWithColor(color: UIColor.white), for: .normal)
        


        
        
    }

}
